#include "foodcontroller.h"

FoodController::FoodController(QObject *parent)
    : QObject{parent}
{
    accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");//构建json头
    //测试post
    //reply = accessManager->post(request, QByteArray("{\"password\": \"123456\", \"phone\": \"15683141454\"}"));
}

//测试通过
void FoodController::addFood(QString token,QString info, QString name, QString pic, QString shelfLife, int type, QString typeName, int unit, QString unitName)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/food/add"));//设置url
     request.setRawHeader("Authorization",token.toLatin1());
     QJsonObject json;
     json.insert("info",info);
     json.insert("name",name);
     json.insert("pic",pic);
     json.insert("shelfLife",shelfLife);
     json.insert("type",type);
     json.insert("typeName",typeName);
     json.insert("unit",unit);
     json.insert("unitName",unitName);
     QJsonDocument jsonDocument;
     jsonDocument.setObject(json);
     postData = jsonDocument.toJson(QJsonDocument::Compact);
     reply = accessManager->post(request,postData);
}
//测试通过（要是出错说明测试太麻烦忘记测试）
void FoodController::updateFood(QString token, QString info, QString name, QString pic, QString shelfLife, int type, QString typeName, int unit, QString unitName, int createId, int createUser, QString gmtCreate, QString gmtModified, int version,QString alias)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/food/update"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json;
    json.insert("info",info);
    json.insert("name",name);
    json.insert("pic",pic);
    json.insert("shelfLife",shelfLife);
    json.insert("type",type);
    json.insert("typeName",typeName);
    json.insert("unit",unit);
    json.insert("unitName",unitName);
    json.insert("alias",alias);
    json.insert("createId",createId);
    json.insert("createUser",createUser);
    json.insert("gmtCreate",gmtCreate);
    json.insert("version",version);
    json.insert("gmtModified",gmtModified);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}
//测试通过
void FoodController::infoFood(QString token, int createId)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/food/info"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json;
    json.insert("createId",createId);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}
//测试通过
void FoodController::searchDefaultFood(QString token, QString name, int index, int size)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/food/searchDefault"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json,json1;
    json1.insert("name",name);
    json.insert("filter",json1);
    json.insert("index",index);
    json.insert("size",size);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}
//测试通过
void FoodController::searchCustomerFood(QString token, QString name, int index, int size)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/food/searchCustom"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json,json1;
    json1.insert("name",name);
    json.insert("filter",json1);
    json.insert("index",index);
    json.insert("size",size);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}

void FoodController::finishedSlot(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();
        //qDebug() << "许凡楠大帅哥";
        //qDebug() << bytes;
        QJsonParseError jsonError;
        QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);  // 转化为 JSON 文档
        QJsonObject object = doucment.object();
        qDebug()<<object;
        if (object.contains("code"))
        {
            QJsonValue value = object.value("code");  // 获取指定 key 对应的 value
            this->m_foodinfo.code=value.toInt();
        }
        if(object.contains("message"))
        {
            QJsonValue value = object.value("message");
            this->m_foodinfo.message=value.toString();
            qDebug()<<this->m_foodinfo.message;
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/food/add")
        {
            //无返回
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/food/update")
        {
            //无返回
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/food/info")
        {
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isObject())
                {
                    QJsonObject qobject=value.toObject();
                    if(qobject.contains("version"))
                    {
                        QJsonValue value = qobject.value("version");
                        this->r_foodinfo.version=value.toInt();
                    }
                    if(qobject.contains("createUser"))
                    {
                        QJsonValue value = qobject.value("createUser");
                        this->r_foodinfo.createUser=value.toInt();
                    }
                    if(qobject.contains("createId"))
                    {
                        QJsonValue value = qobject.value("createId");
                        this->r_foodinfo.createId=value.toInt();
                    }
                    if(qobject.contains("type"))
                    {
                        QJsonValue value = qobject.value("type");
                        this->r_foodinfo.type=value.toInt();
                    }
                    if(qobject.contains("shelfLife"))
                    {
                        QJsonValue value = qobject.value("shelfLife");
                        this->r_foodinfo.shelfLife=value.toInt();
                    }
                    if(qobject.contains("unit"))
                    {
                        QJsonValue value = qobject.value("unit");
                        this->r_foodinfo.unit=value.toInt();
                    }
                    if(qobject.contains("gmtCreate"))
                    {
                        QJsonValue value = qobject.value("gmtCreate");
                        this->r_foodinfo.gmtCreate=value.toString();
                    }
                    if(qobject.contains("gmtModified"))
                    {
                        QJsonValue value = qobject.value("gmtModified");
                        this->r_foodinfo.gmtModified=value.toString();
                    }
                    if(qobject.contains("name"))
                    {
                        QJsonValue value = qobject.value("name");
                        this->r_foodinfo.name=value.toString();
                    }
                    if(qobject.contains("info"))
                    {
                        QJsonValue value = qobject.value("info");
                        this->r_foodinfo.info=value.toString();
                    }
                    if(qobject.contains("alias"))
                    {
                        QJsonValue value = qobject.value("alias");
                        this->r_foodinfo.alias=value.toString();
                    }
                    if(qobject.contains("pic"))
                    {
                        QJsonValue value = qobject.value("pic");
                        this->r_foodinfo.pic=value.toString();
                    }
                }
            }

            emit infoFoodsuccess();
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/food/searchDefault")
        {
            this->s_foodlist.clear();
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isObject())
                {
                    QJsonObject qobject=value.toObject();
                    if(qobject.contains("rows"))
                    {
                        QJsonValue value = qobject.value("rows");
                        if(value.isArray())
                        {
                            QJsonArray array = value.toArray();
                            int nSize = array.size();
                            for(int i=0;i<nSize;i++)
                            {
                                QJsonValue value = array.at(i);
                                QJsonObject qobject = value.toObject();
                                sFoodInfo sfoodinfo;
                                if(qobject.contains("name"))
                                {
                                     QJsonValue value = qobject.value("name");
                                     sfoodinfo.name=value.toString();
                                }
                                if(qobject.contains("type"))
                                {
                                     QJsonValue value = qobject.value("type");
                                     sfoodinfo.type=value.toString();
                                }
                                if(qobject.contains("unit"))
                                {
                                     QJsonValue value = qobject.value("unit");
                                     sfoodinfo.unit=value.toString();
                                }
                                if(qobject.contains("info"))
                                {
                                     QJsonValue value = qobject.value("info");
                                     sfoodinfo.info=value.toString();
                                }
                                if(qobject.contains("alias"))
                                {
                                     QJsonValue value = qobject.value("alias");
                                     sfoodinfo.alias=value.toString();
                                }
                                if(qobject.contains("pic"))
                                {
                                     QJsonValue value = qobject.value("pic");
                                     sfoodinfo.pic=value.toString();
                                }
                                if(qobject.contains("userId"))
                                {
                                     QJsonValue value = qobject.value("userId");
                                     sfoodinfo.userId=value.toInt();
                                }
                                if(qobject.contains("foodId"))
                                {
                                     QJsonValue value = qobject.value("foodId");
                                     sfoodinfo.foodId=value.toInt();
                                }
                                if(qobject.contains("shelfLife"))
                                {
                                     QJsonValue value = qobject.value("shelfLife");
                                     sfoodinfo.shelfLife=value.toInt();
                                }
                                this->s_foodlist.push_back(sfoodinfo);
                            }
                        }
                    }
                }
            }
            emit searchDefaultsuccess();
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/food/searchCustom")
        {
            this->s_foodlist.clear();
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isObject())
                {
                    QJsonObject qobject=value.toObject();
                    if(qobject.contains("rows"))
                    {
                        QJsonValue value = qobject.value("rows");
                        if(value.isArray())
                        {
                            QJsonArray array = value.toArray();
                            int nSize = array.size();
                            for(int i=0;i<nSize;i++)
                            {
                                QJsonValue value = array.at(i);
                                QJsonObject qobject = value.toObject();
                                sFoodInfo sfoodinfo;
                                if(qobject.contains("name"))
                                {
                                     QJsonValue value = qobject.value("name");
                                     sfoodinfo.name=value.toString();
                                }
                                if(qobject.contains("type"))
                                {
                                     QJsonValue value = qobject.value("type");
                                     sfoodinfo.type=value.toString();
                                }
                                if(qobject.contains("unit"))
                                {
                                     QJsonValue value = qobject.value("unit");
                                     sfoodinfo.unit=value.toString();
                                }
                                if(qobject.contains("info"))
                                {
                                     QJsonValue value = qobject.value("info");
                                     sfoodinfo.info=value.toString();
                                }
                                if(qobject.contains("alias"))
                                {
                                     QJsonValue value = qobject.value("alias");
                                     sfoodinfo.alias=value.toString();
                                }
                                if(qobject.contains("pic"))
                                {
                                     QJsonValue value = qobject.value("pic");
                                     sfoodinfo.pic=value.toString();
                                }
                                if(qobject.contains("userId"))
                                {
                                     QJsonValue value = qobject.value("userId");
                                     sfoodinfo.userId=value.toInt();
                                }
                                if(qobject.contains("foodId"))
                                {
                                     QJsonValue value = qobject.value("foodId");
                                     sfoodinfo.foodId=value.toInt();
                                }
                                if(qobject.contains("shelfLife"))
                                {
                                     QJsonValue value = qobject.value("shelfLife");
                                     sfoodinfo.shelfLife=value.toInt();
                                }
                                this->s_foodlist.push_back(sfoodinfo);
                            }
                        }
                    }
                }
            }
            //qDebug()<<this->s_foodlist[0].shelfLife;
            emit searchCustomersuccess();
        }
        emit foodcontrollersuccess();
    }
    else
    {
        qDebug() << "finishedSlot errors here";
        qDebug( "found error .... code: %d\n", (int)reply->error());
        qDebug(qPrintable(reply->errorString()));
    }
    reply->deleteLater();
}
